function words(s) {
  const sLen = s.length;
  return {
    [Symbol.iterator]() {
      let index = 0;
      return {
        next() {
          let startIndex = index;
          while (startIndex < sLen && s.charCodeAt(startIndex) === 32) {
            startIndex += 1;
          }
          if (startIndex >= sLen) return { done: true };
          let endIndex = startIndex + 1;
          while (endIndex < sLen && s.charCodeAt(endIndex) !== 32) {
            endIndex += 1;
          }
          const value = s.slice(startIndex, endIndex);
          index = endIndex;
          return { value };
        },
      };
    },
  };
}

console.log([...words("  abc def   ghi!  ")]); // [ 'abc', 'def', 'ghi!' ]

console.log([..."  abc def   ghi!  ".matchAll(/[^ ]+/g)].map((e) => e[0])); // [ 'abc', 'def', 'ghi!' ]
